package com.ruoyi.saleReturn.controller;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.audit.domain.Audit;
import com.ruoyi.audit.service.AuditService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.company.domain.CompanyInvoice;
import com.ruoyi.company.domain.CompanyOther;
import com.ruoyi.company.service.ICompanyInvoiceService;
import com.ruoyi.company.service.ICompanyOtherServer;
import com.ruoyi.procurement.domain.ProcurementModel;
import com.ruoyi.procurement.service.ProcurementServer;
import com.ruoyi.product.domain.Product;
import com.ruoyi.product.domain.ProductModel;
import com.ruoyi.purchase.service.PurchaseOrderService;
import com.ruoyi.purchaseReturn.domain.PurchaseReturnModel;
import com.ruoyi.purchaseReturn.domain.PurchaseReturnOrder;
import com.ruoyi.purchaseReturn.mapper.PurchaseReturnMapper;
import com.ruoyi.purchaseReturn.service.IPurchaseReturnService;
import com.ruoyi.sale.domain.SaleGoodsModel;
import com.ruoyi.sale.domain.SaleGoodsOrder;
import com.ruoyi.sale.domain.SaleorderOrder;
import com.ruoyi.sale.mapper.SaleGoodsMapper;
import com.ruoyi.sale.service.ISaleGoodsService;
import com.ruoyi.sale.service.ISaleorderService;
import com.ruoyi.saleReturn.domain.SaleReturnModel;
import com.ruoyi.saleReturn.domain.SaleReturnOrder;
import com.ruoyi.saleReturn.service.ISaleReturnService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jndj
 * @date 2022-06-17
 */
@RestController
@RequestMapping("/business/saleReturn")
public class SaleReturnController extends BaseController {

    @Autowired
    ISaleorderService iSaleorderService;
    @Autowired
    ICompanyInvoiceService iCompanyInvoiceService;
    @Autowired
    ICompanyOtherServer iCompanyOtherServer;
    @Autowired
    PurchaseOrderService purchaseOrderService;
    @Autowired
    private ISaleReturnService iSaleReturnService;
    @Autowired
    private SaleGoodsMapper saleGoodsMapper;
    @Autowired
    private ISaleGoodsService iSaleGoodsService;
    @Autowired
    private IPurchaseReturnService iPurchaseReturnService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ProcurementServer procurementServer;
    @Autowired
    private AuditService auditService;
    @Autowired
    private PurchaseReturnMapper purchaseReturnMapper;

    private String prefix = "saleReturn";

    @RequiresPermissions("business:saleReturn:view")
    @GetMapping()
    public String saleReturn(Model model) {
        Long companyId = ShiroUtils.getSysUser().getCompanyId();
        List<SaleReturnOrder> list = iSaleReturnService.selectSaleReturnOrderListCustObj(companyId);
        model.addAttribute("custObj", list);
        return prefix + "/list";
    }

    /**
     * 销售退货 查询列表
     */
    @RequiresPermissions("business:saleReturn:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(value = "companyName", required = false) String companyName,
                              @RequestParam(value = "saleReturnStatus", required = false) String saleReturnStatus,
                              @RequestParam(value = "beginTime", required = false) String beginTime,
                              @RequestParam(value = "endTime", required = false) String endTime) {
        SaleReturnOrder saleReturnOrder = new SaleReturnOrder();
        saleReturnOrder.setCompanyName(companyName);
        saleReturnOrder.setSaleReturnStatus(saleReturnStatus);
        saleReturnOrder.setBeginTime(beginTime);
        saleReturnOrder.setEndTime(endTime);
        startPage();
        saleReturnOrder.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<SaleReturnOrder> list = iSaleReturnService.selectSaleReturnOrderList(saleReturnOrder);
        for (SaleReturnOrder saleReturnOrder1 : list) {
            if (saleReturnOrder1.getSaleGoodsNum() == null || saleReturnOrder1.getSaleGoodsNum().isEmpty()) {
                saleReturnOrder1.setSaleGoodsNum(saleReturnOrder1.getProcurementWarehousingName());
            }

        }
        return getDataTable(list);
    }

    /**
     * 新增 销售退货
     */
    @GetMapping("/selectInformation")
    public Map<String, Object> add() {
        Map<String, Object> resMap = new HashMap<>();
        Long companyId = SecurityUtils.getLoginUser().getUser().getCompanyId();
        List<SysUser> sysUsers = iSysUserService.selectSupplierById(companyId);
        List<SysUser> user = iSysUserService.selectSalesmanList(companyId);
        if (sysUsers != null) {
            resMap.put("sysUsers", sysUsers);
        }
        if (user != null) {
            resMap.put("user", user);
        }
        return resMap;
    }

    /**
     * 修改销售退货
     */
    @GetMapping("/edit/{saleReturnModelId}")
    public String edit(@PathVariable("saleReturnModelId") Long saleReturnModelId, ModelMap mmap) {
        SaleReturnOrder saleReturnOrder = iSaleReturnService.selectSaleReturnOrderById(saleReturnModelId);
        mmap.put("saleReturnOrder", saleReturnOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存销售退货
     */
//    @RequiresPermissions("business:saleReturn:edit")
//    @Log(title = "修改保存销售退货单", businessType = BusinessType.UPDATE)
//    @PostMapping("/edit")
//    @ResponseBody
//    public AjaxResult editSave(SaleReturnOrder saleReturnOrder)
//    {
//        return toAjax(iSaleReturnService.updateSaleReturnOrder(saleReturnOrder));
//    }

    /**
     * 选择 销货单界面
     */
    @GetMapping("/selectSaleGoods")
    public Map<String, Object> selectProcurement() {
        Map<String, Object> resMap = new HashMap<>();
        List<SaleGoodsOrder> saleGoodsOrders = iSaleGoodsService.selectSaleGoodsCustObjByStatus(SecurityUtils.getLoginUser().getUser().getCompanyId());
        for (int i = saleGoodsOrders.size() - 1; i >= 0; i--) {
            SaleGoodsOrder saleGoodsOrder = saleGoodsOrders.get(i);
            if (saleGoodsOrder == null) {
                saleGoodsOrders.remove(i);
            }
        }
        List<SaleGoodsOrder> saleGoodsOrders1 = new ArrayList<>();
        saleGoodsOrders1 = saleGoodsOrders;
        resMap.put("custObj", saleGoodsOrders1);
        return resMap;
    }

    /**
     * 选择销货单列表 post
     */
    @GetMapping("/selectSaleGoodsOrderList")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(value = "customerId", required = false) Long customerId,
                                    @RequestParam(value = "beginTime", required = false) String beginTime,
                                    @RequestParam(value = "endTime", required = false) String endTime) {
        Map<String, Object> resMap = new HashMap<>();
        SaleGoodsOrder saleGoodsOrder = new SaleGoodsOrder();
        saleGoodsOrder.setCustomerId(customerId);
        saleGoodsOrder.setBeginTime(beginTime);
        saleGoodsOrder.setEndTime(endTime);
        startPage();
        saleGoodsOrder.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<SaleGoodsOrder> list = iSaleGoodsService.selectSaleGoodsByStatus(saleGoodsOrder);
        resMap.put("rows", list);
        resMap.put("code", 200);
        return resMap;
    }

    /**
     * 选择销货单 子表的数据
     */
    @GetMapping("/selectSaleGoodsModelList")
    @ResponseBody
    public Map<String, Object> selectProcurementModelList(@RequestParam(value = "saleGoodsId", required = false) Long saleGoodsId) {
        startPage();
        Map<String, Object> resMap = new HashMap<>();
        List<SaleGoodsModel> saleGoodsModels = iSaleReturnService.selectShowSaleGoodsModelById(saleGoodsId);
        for (SaleGoodsModel saleGoodsModel : saleGoodsModels) {
            if (saleGoodsModel.getUnit().equals("盒") || saleGoodsModel.getUnit() == "盒") {
                saleGoodsModel.setRemainingNum(saleGoodsModel.getRemainingNum() / saleGoodsModel.getFactor().intValue());
            }
        }
        resMap.put("rows", saleGoodsModels);
        resMap.put("code", 200);
        return resMap;
    }

    @PostMapping("/selectSaleGoodsModelById")
    @ResponseBody
    public SaleGoodsOrder selectSaleGoodsModelById(Long saleGoodsId) {
        HashMap hashMap = new HashMap();
        hashMap.put("saleGoodsId", saleGoodsId);
        SaleGoodsOrder saleGoodsOrder = iSaleGoodsService.selectSaleGoodsById(hashMap);
        return saleGoodsOrder;
    }

    /**
     * 选择产品批次 带出数据
     */
    @PostMapping("/selectSaleGoodsModelToTable")
    @ResponseBody
    public List<SaleGoodsModel> selectSaleGoodsModelToTable(String data, Long saleGoodsId) {
        JSONArray jsonArray = JSONArray.parseArray(data);
        List<SaleGoodsModel> saleGoodsModels = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            com.alibaba.fastjson.JSONObject jsonObject = (com.alibaba.fastjson.JSONObject) jsonArray.get(i);
            //唯一码
            Object modelCode = jsonObject.get("modelCode");
            //批次
            Object batch = jsonObject.get("batch");
            //出库数量
            Object num = jsonObject.get("num");
            Object saleGoodsModelId = jsonObject.get("saleGoodsModelId");
            HashMap<Object, Object> hashMap = new HashMap<>();
            hashMap.put("modelCode", modelCode);
            hashMap.put("batch", batch);
            hashMap.put("saleGoodsId", saleGoodsId);
            hashMap.put("saleGoodsModelId", saleGoodsModelId);
            hashMap.put("deptId", ShiroUtils.getSysUser().getCompanyId());
            //退货数量
            hashMap.put("num", num);
            SaleGoodsModel saleGoodsModel = iSaleReturnService.selectSaleGoodsModel(hashMap);
            saleGoodsModels.add(saleGoodsModel);
        }
        return saleGoodsModels;
    }

    /**
     * 选择 退货单界面
     */
    @GetMapping("/selectPurchaseReturn")
    public Map<String, Object> selectPurchaseReturn() {
        Map<String, Object> resMap = new HashMap<>();
        List<SaleorderOrder> list = iSaleReturnService.selectPurchaseReturnCustObj(SecurityUtils.getLoginUser().getCompanyId());
        resMap.put("SupplierObj", list);
        return resMap;
    }

    /**
     * 选择销货单列表 post
     */
    @GetMapping("/selectPurchaseReturnList")
    @ResponseBody
    public TableDataInfo selectPurchaseReturnList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                                  @RequestParam(value = "endTime", required = false) String endTime,
                                                  @RequestParam(value = "deptId", required = false) Long deptId) {
        startPage();
        PurchaseReturnOrder purchaseReturnOrder = new PurchaseReturnOrder();
        purchaseReturnOrder.setBeginTime(beginTime);
        purchaseReturnOrder.setEndTime(endTime);
        purchaseReturnOrder.setDeptId(deptId);
        purchaseReturnOrder.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
        purchaseReturnOrder.setIsReturn("0");
        //审核通过的
        List<PurchaseReturnOrder> list = iSaleReturnService.selectPurchaseReturnList(purchaseReturnOrder);
        return getDataTable(list);
    }

    /**
     * 选择销货单 子表的数据
     */
    @GetMapping("/selectPurchaseReturnModelList")
    @ResponseBody
    public TableDataInfo selectPurchaseReturnModelList(@RequestParam(value = "purchaseReturnId", required = false) Long purchaseReturnId) {
        startPage();
        List<PurchaseReturnModel> purchaseReturnModels = iSaleReturnService.selectShowPurchaseReturnModelById(purchaseReturnId);
        ProcurementModel procurementModel = new ProcurementModel();
        List<ProcurementModel> procurementModels = new ArrayList<>();
        for (int i = 0; i < purchaseReturnModels.size(); i++) {
            PurchaseReturnModel purchaseReturnModel = purchaseReturnModels.get(i);
            Long procurementWarehousingNo = purchaseReturnModel.getProcurementWarehousingNo();
            procurementModel = iPurchaseReturnService.selectProcurementModelByNo(procurementWarehousingNo);
            if (procurementModel.getUnit().equals("盒") || procurementModel.getUnit() == "盒") {
                if (purchaseReturnModel.getRemainingNum() != 0) {
                    procurementModel.setRemainingNum(purchaseReturnModel.getRemainingNum() / procurementModel.getFactor().intValue());
                } else {
                    procurementModel.setRemainingNum(purchaseReturnModel.getRemainingNum());
                }

            } else {
                procurementModel.setRemainingNum(purchaseReturnModel.getRemainingNum());
            }
            procurementModel.setPurchaseReturnId(purchaseReturnModel.getPurchaseReturnId());
            procurementModel.setPurchaseReturnModelId(purchaseReturnModel.getPurchaseReturnModelId());
            procurementModel.setAllNum(Long.valueOf(purchaseReturnModels.get(i).getAllNum()));
            procurementModels.add(procurementModel);

        }
        return getDataTable(procurementModels);
    }

    @PostMapping("/selectPurchaseReturnById")
    @ResponseBody
    public List<PurchaseReturnModel> selectPurchaseReturnById(Long purchaseReturnId) {
        PurchaseReturnOrder purchaseReturnOrder = iPurchaseReturnService.selectPurchaseReturnById(purchaseReturnId);
        if (purchaseReturnOrder.getPurchaseReturnModels() != null) {
            for (int i = 0; i < purchaseReturnOrder.getPurchaseReturnModels().size(); i++) {
                PurchaseReturnModel purchaseReturnModel = purchaseReturnOrder.getPurchaseReturnModels().get(i);
                Long procurementWarehousingNo = purchaseReturnModel.getProcurementWarehousingNo();
                ProcurementModel procurementModel = iPurchaseReturnService.selectProcurementModelByNo(procurementWarehousingNo);
                purchaseReturnModel.setProcurementModel(procurementModel);
            }
            return purchaseReturnOrder.getPurchaseReturnModels();
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 新增保存销售退货订单
     */
    @RequiresPermissions("business:saleReturn:add")
    @Transactional
    @Log(title = "新增保存销售退货订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addPost(@RequestBody SaleReturnOrder saleReturnOrder) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if (saleReturnOrder.getSaleReturnModels() == null) {
            return AjaxResult.error("请选择退货的产品");
        }

        if (saleReturnOrder.getSaleGoodsId() != null && !"".equals(saleReturnOrder.getSaleGoodsId())) {
            saleReturnOrder.setSaleReturnStatus("0");
            saleReturnOrder.setSaleReturnDate(DateUtils.getNowDate());
            saleReturnOrder.setCreateTime(DateUtils.getNowDate());
            saleReturnOrder.setCreateBy(sysUser.getNickName());
            saleReturnOrder.setDeptId(sysUser.getCompanyId());
            saleReturnOrder.setCompanyId(saleReturnOrder.getCustomerId());
        } else {
            saleReturnOrder.setSaleReturnStatus("0");
            saleReturnOrder.setSaleReturnDate(DateUtils.getNowDate());
            saleReturnOrder.setCreateTime(DateUtils.getNowDate());
            saleReturnOrder.setCreateBy(sysUser.getNickName());
            saleReturnOrder.setDeptId(sysUser.getCompanyId());
        }
        //创建采购订单
        int i = iSaleReturnService.insertSaleReturnOrder(saleReturnOrder);

        if (saleReturnOrder.getSaleReturnModels() != null) {
            List<SaleReturnModel> saleReturnModels = saleReturnOrder.getSaleReturnModels();
            for (int j = 0; j < saleReturnModels.size(); j++) {
                SaleReturnModel saleReturnModel = saleReturnModels.get(j);
                saleReturnModel.setSaleReturnId(saleReturnOrder.getSaleReturnId());
                iSaleReturnService.insertSaleReturnModel(saleReturnModel);
            }
        }

        //添加审核订单
        Audit audit = new Audit();
        //销售订单Id
        audit.setOrderId(saleReturnOrder.getSaleReturnId());
        //登陆人的companyid
        audit.setCompanyId(sysUser.getCompanyId());
        //主表的含税金额
        audit.setTaxAmount(saleReturnOrder.getTaxAmount());
        //业务员id
        audit.setUserId(saleReturnOrder.getUserId());
        //客户id
        audit.setSupplierId(saleReturnOrder.getCompanyId());
        //订单编号
        audit.setOrderNum("".equals(saleReturnOrder.getSaleGoodsNum()) ? saleReturnOrder.getProcurementWarehousingName() : saleReturnOrder.getSaleGoodsNum());

        //6为 销售退货
        audit.setOrderType("6");
        //状态0为 未审核
        audit.setAuditStatus("0");
        int insertAudit = auditService.insertAudit(audit);

        //若 选的是 销货单 则改变销货单 将是否生成退货单改为1
        //若 选的是 采购退货单 则改变采购退货单 将是否生成退货单改为1

        if (saleReturnOrder.getSaleGoodsId() != null) {
            List<SaleGoodsModel> saleGoodsModels = saleGoodsMapper.selectReturnNum(saleReturnOrder.getSaleGoodsId());
            List<SaleGoodsModel> saleGoodsModelList = new ArrayList<>();
            int count = 0;
            if (saleGoodsModels.size() == saleReturnOrder.getSaleReturnModels().size()) {
                SaleGoodsModel saleGoodsModel = new SaleGoodsModel();
                for (int j = 0; j < saleGoodsModels.size(); j++) {
                    if (saleGoodsModels.get(j).getRemainingNum() != 0) {
                        saleGoodsModel = saleGoodsMapper.selectSalegoodsModelReturn(saleGoodsModels.get(j).getSaleGoodsModelId());
                        saleGoodsModel.setRemainingNum(saleGoodsModels.get(j).getRemainingNum() - saleReturnOrder.getSaleReturnModels().get(j).getAllNum());
                        saleGoodsModelList.add(saleGoodsModel);
                        saleGoodsMapper.updateSaleGoodsModel(saleGoodsModel);

                    }
                }
                for (SaleGoodsModel saleGoodsModel1 : saleGoodsModelList) {
                    count += saleGoodsModel1.getRemainingNum();
                }
                //当副表里的数据全部退货以后才修改主表里的退货状态
                if (count == 0) {
                    //改变销货单的状态
                    HashMap hashMap = new HashMap();
                    hashMap.put("status", 1);
                    hashMap.put("saleGoodsId", saleReturnOrder.getSaleGoodsId());
                    int a = iSaleGoodsService.updateSaleGoodsStatus(hashMap);
                }
            }
        } else {
            List<PurchaseReturnModel> purchaseReturnModels = purchaseReturnMapper.selectPurchaseReturnModel(saleReturnOrder.getPurchaseReturnId());
            List<PurchaseReturnModel> purchaseReturnModelList = new ArrayList<>();
            int count = 0;
            if (purchaseReturnModels.size() == saleReturnOrder.getSaleReturnModels().size()) {
                PurchaseReturnModel purchaseReturnModel = new PurchaseReturnModel();
                for (int j = 0; j < purchaseReturnModels.size(); j++) {
                    if (purchaseReturnModels.get(j).getRemainingNum() != 0) {
                        purchaseReturnModel = purchaseReturnMapper.selectPurchaseReturnModelById(purchaseReturnModels.get(j).getPurchaseReturnModelId());
                        purchaseReturnModel.setRemainingNum(purchaseReturnModels.get(j).getRemainingNum() - saleReturnOrder.getSaleReturnModels().get(j).getAllNum());
                        purchaseReturnModelList.add(purchaseReturnModel);
                        purchaseReturnMapper.updatePurchaseReturnModel(purchaseReturnModel);
                    }
                }
                for (PurchaseReturnModel purchaseReturnModel1 : purchaseReturnModelList) {
                    count += purchaseReturnModel1.getRemainingNum();
                }
                if (count == 0) {
                    //改变采购退货的状态
                    HashMap hashMap = new HashMap();
                    hashMap.put("status", 1);
                    hashMap.put("purchaseReturnId", saleReturnOrder.getPurchaseReturnId());
                    int a = iPurchaseReturnService.updatePurchaseReturnStatus(hashMap);
                }
            }

        }
        if (insertAudit >= 1 && i >= 1) {
            return toAjax(1);
        } else {
            return toAjax(-1);
        }
    }

    /**
     * 删除销售退货订单
     */
    @RequiresPermissions("business:saleReturn:remove")
    @Log(title = "删除销售退货订单", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{saleReturnId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("saleReturnId") Long saleReturnId) {
        return toAjax(iSaleReturnService.deleteSaleReturnOrderById(saleReturnId));
    }

    /**
     * 查看销售退货订单
     */
    @GetMapping("/findSaleReturn/{saleReturnId}")
    public Map<String, Object> findSaleReturn(@PathVariable("saleReturnId") Long saleReturnId) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Map<String, Object> resMap = new HashMap<>();
        SaleReturnOrder saleReturnOrder = iSaleReturnService.selectSaleReturnOrderAndModelById(saleReturnId);
        List<SaleGoodsModel> saleGoodsModels = new ArrayList<>();
        if (saleReturnOrder.getSaleReturnModels().get(0).getSaleReturnId() == null) {
            List<SaleReturnModel> list = new ArrayList();
            saleReturnOrder.setSaleReturnModels(list);
            resMap.put("saleReturnOrder", saleReturnOrder);
        } else {
            List<ProcurementModel> procurementModels = new ArrayList<>();
            //查询 采购入库单的信息 set进model 中
            List<SaleReturnModel> saleReturnModels = saleReturnOrder.getSaleReturnModels();
            if (saleReturnOrder.getPurchaseReturnId() != null) {
                for (int i = 0; i < saleReturnModels.size(); i++) {
                    SaleReturnModel saleReturnModel = saleReturnModels.get(i);
                    //采购入库从表 信息
                    PurchaseReturnModel purchaseReturnModel = iPurchaseReturnService.selectPurchaseReturnModelById(saleReturnModel.getPurchaseReturnModelId());
                    Long procurementWarehousingNo = purchaseReturnModel.getProcurementWarehousingNo();
                    ProcurementModel procurementModel = iPurchaseReturnService.selectProcurementModelByNo(procurementWarehousingNo);
                    if (purchaseReturnModel.getUnit().equals("盒")) {
                        if (purchaseReturnModel.getRemainingNum() != 0) {
                            procurementModel.setRemainingNum(purchaseReturnModel.getRemainingNum() / procurementModel.getFactor().intValue());
                        } else {
                            procurementModel.setRemainingNum(purchaseReturnModel.getRemainingNum());
                        }

                    } else {
                        procurementModel.setRemainingNum(purchaseReturnModel.getRemainingNum());
                    }
                    saleReturnModel.setProcurementModel(procurementModel);
                    procurementModel.setOutNum(saleReturnModel.getReturnNum());
                    procurementModel.setNum(procurementModel.getPurchaseNum());
                    procurementModels.add(procurementModel);
                }
                resMap.put("saleGoodsModel", procurementModels);
            } else {
                for (int i = 0; i < saleReturnModels.size(); i++) {
                    SaleReturnModel saleReturnModel = saleReturnModels.get(i);
                    SaleGoodsModel saleGoodsModel = iSaleGoodsService.selectSaleGoodsModelByModelId(saleReturnModel.getSaleGoodsModelId());
                    if (saleGoodsModel.getUnit().equals("盒") || saleGoodsModel.getUnit() == "盒") {
                        if (saleGoodsModel.getRemainingNum() != 0) {
                            saleGoodsModel.setRemainingNum(saleGoodsModel.getRemainingNum() / saleGoodsModel.getFactor().intValue());
                        }

                    }
                    saleGoodsModel.setOutNum(saleReturnModel.getReturnNum());
                    saleGoodsModels.add(saleGoodsModel);
                    //saleReturnModel.setSaleGoodsModel(saleGoodsModel);
                }
                resMap.put("saleGoodsModel", saleGoodsModels);
            }
            if ("".equals(saleReturnOrder.getSaleGoodsNum())) {
                saleReturnOrder.setSaleGoodsNum(null);
            }
            if ("".equals(saleReturnOrder.getProcurementWarehousingName())) {
                saleReturnOrder.setProcurementWarehousingName(null);
            }
            resMap.put("saleReturnOrder", saleReturnOrder);
        }
        Product product = new Product();
        product.setDeptId(sysUser.getCompanyId());
        Map<String, Object> map1 = new HashMap<>();
        map1.put("deptId", sysUser.getCompanyId());
        List<ProductModel> list = iSaleorderService.selectProductList(map1);
        CompanyInvoice companyInvoice = iCompanyInvoiceService.selectByCompanyId(sysUser.getCompanyId());
        if (list != null) {
            resMap.put("products", list);
        }
        if (companyInvoice != null) {

            resMap.put("taxRate", companyInvoice.getTaxRate());
        }

        // 获取上一条临近的ID
        resMap.put("previousId", iSaleReturnService.querypreviousId(saleReturnId));
        // 获取下一条临近的ID
        resMap.put("nextId", iSaleReturnService.queryNextId(saleReturnId));
        return resMap;
    }

    /**
     * 查询供应商的绑定业务员
     */
    @GetMapping("/selectUser/{companyId}")
    @ResponseBody
    public AjaxResult selectUser(@PathVariable("companyId") Long companyId) {
        Map<String, Object> map = new HashMap<>();
        map.put("deptId", SecurityUtils.getLoginUser().getCompanyId());
        map.put("companyId", companyId);
        CompanyOther companyOther = iCompanyOtherServer.selectByCompanyIdAndDeptId(map);
        if (companyOther != null) {
            return AjaxResult.success(companyOther);
        } else {
            return AjaxResult.error("该公司没有合作单位请设置负责人信息");
        }
    }

    /**
     * 修改订单状态
     * @param saleReturnId
     * @param status
     * @return
     */
    @Log(title = "销售退货审核", businessType = BusinessType.UPDATE)
    @GetMapping("/auditSale")
    @ResponseBody
    public AjaxResult auditSale(@RequestParam(value = "saleReturnId", required = false) Long saleReturnId,
                                @RequestParam(value = "status", required = false) String status) {

        int result = iSaleReturnService.auditSale(saleReturnId, status);
        return toAjax(result);
    }

}
